const { imgUrl } = getApp().globalData;
import { publicApi } from "@/utils/request/api/public";
import { mineApi } from "@/utils/request/api/mine";
Page({
  data: {
    imgUrl: imgUrl + "mine/",
    type: 'add',
    addCon: {
      company_id: Number(wx.getStorageSync('company_id')) || null, //公司id
      party_a_name: "", //甲方名称
      name: "", //合同名称
      amount: "", //合同金额,元
      sign_date: "", //签订时间
      expire_date: "", //到期时间
      contract_url: "", //合同文件
      bid_notice_url: "", //中标通知文件
      evaluation_url: "", //履约评价文件
      summary: "", //摘要内容
    },
    isMore1: false,
    isMore2: false,
    isMore3: false,
    showImageUrl: ''
  },
  afterRead() {},
  change(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let addCon = JSON.parse(JSON.stringify(this.data.addCon));
    addCon[type] = event.detail.value;
    this.setData({
      addCon,
    });
  },
  select(event) {
    const {
      currentTarget: {
        dataset: { type, origin },
      },
    } = event;
    let addCon = JSON.parse(JSON.stringify(this.data.addCon));
    addCon[type] = this.data[addCon][event.detail.value / 1];
    this.setData({
      addCon,
    });
  },
  selectDate(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let addCon = JSON.parse(JSON.stringify(this.data.addCon));
    addCon[type] = event.detail.value;
    this.setData({
      addCon,
    });
  },
  showMore(event) {
    let index = event.currentTarget.dataset.index;
    if (index == 1) {
      this.setData({
        isMore1: !this.data.isMore1,
        isMore2:false,
        isMore3:false
      })
    } else if (index == 2) {
      this.setData({
        isMore2: !this.data.isMore2,
        isMore1:false,
        isMore3:false
      })
    } else if (index == 3) {
      this.setData({
        isMore3: !this.data.isMore3,
        isMore1:false,
        isMore2:false
      })
    }
  },
  hideMore() {
    this.setData({
      isMore1:false,
      isMore2:false,
      isMore3:false
    })
  },
  handlePreImg(event) {
    console.log(event)
    let index = event.currentTarget.dataset.index;
    if (index == 1) {
      this.setData({
        showImageUrl: this.data.addCon.contract_url,
        showPre: true
      })
    }
  },
  onClose() {
    this.setData({
      showPre: false,
    })
  },
  chooseFile(event) {
      const index = event.currentTarget.dataset.index;
      const that = this;
      wx.chooseMessageFile({
        count: 1,
        type: "file",
        extension: ["pdf", "docx", "jpg", "png", "jpeg"],
        success(res) {
          const filePath = res.tempFiles[0].path;
          const formData = {
            fileVal: filePath,
            name: res.tempFiles[0].name,
          };
  
          publicApi
            .uploadFile({
              filePath,
              formData,
            })
            .then((res2) => {
              let obj = JSON.parse(JSON.stringify(that.data.addCon));
              if (index == 1) {
                obj.contract_url = res2.data;
              } else if (index == 2) {
                obj.bid_notice_url = res2.data;
              } else if (index == 3) {
                obj.evaluation_url = res2.data;
              }

              that.setData({
                add: obj,
              });
            });
        },
      });
    },
    check() {
      let add = JSON.parse(JSON.stringify(this.data.addCon));
      if (!add.party_a_name) {
        wx.showToast({
          title: "甲方名称不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.name) {
        wx.showToast({
          title: "合同名称不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.amount) {
        wx.showToast({
          title: "合同金额不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.sign_date) {
        wx.showToast({
          title: "签订时间不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.expire_date) {
        wx.showToast({
          title: "到期时间不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.contract_url) {
        wx.showToast({
          title: "请上传合同文件!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.bid_notice_url) {
        wx.showToast({
          title: "请上传中标通知文件!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.evaluation_url) {
        wx.showToast({
          title: "请上传履约评价文件!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (!add.summary) {
        wx.showToast({
          title: "摘要内容不能为空!",
          icon: "error",
          duration: 2000,
        })
        return false;
      } else if (add.summary.length > 200) {
        wx.showToast({
          title: "摘要内容不能超过200字!",
          icon: "error",
          duration: 2000,
        })
        return false;
      }
      return true;
    },
  confirmAdd() {
    if (!this.check()) {
      return;
    }
    let add = JSON.parse(JSON.stringify(this.data.addCon));
    if (this.data.type == 'add') {
      add.id = 0;
    }
    add.amount = Number(add.amount)
    let title = '添加成功';
    if (this.data.type === 'eidt') {
      title = '修改成功'
    }
    mineApi.addCompanyHt(add).then(res => {
      wx.showToast({
        title,
        icon: "success",
        duration: 2000,
        success: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            });
          }, 2000);
        }
      })
    })
  },
  confirmEdit() {
    this.confirmAdd()
  },
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('edit', data => {
      console.log('data === ', data)
      this.setData({
        addCon: data.data,
        type: 'edit'
      }, () => {
        wx.setNavigationBarTitle({
          title: '修改业绩合同'
        });
      })
    });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
